package CreatorUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import persistent.*;
import LoginUtil.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db4o.ObjectContainer;
import java.lang.NumberFormatException;

/**
 * Servlet implementation class NewTreatmentRecordServlet
 */
public class NewTreatmentRecordServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) 
					throws ServletException, java.io.IOException {
		try{
			//Get name of patient, nurse, doctor
			String patientname = request.getParameter("patientname");
			String doctorname = request.getParameter("doctorname");
			String nursename = request.getParameter("nursename");
			//Create database reference
		    ObjectContainer container = 
			        (ObjectContainer)request.getAttribute(Db4oServletListener.KEY_DB4O_SESSION);
			//Get nurse, patient, user with those usernames
		    User predicate = new User();
		    predicate.setUserName(patientname);
		    User patient = (User)container.queryByExample(predicate).next();
		    predicate.setUserName(doctorname);
		    User doctor = (User)container.queryByExample(predicate).next();
		    predicate.setUserName(nursename);
		    User nurse = (User)container.queryByExample(predicate).next();
		    
		    //Create TreatmentRecord object
		    TreatmentRecord newRecord = new TreatmentRecord(doctor, patient, nurse);
		    
		    //Insert all data into TreatmentRecord
		    
		    // attempt to parse and set the weight
		    try{
		    	newRecord.setWeight(Integer.parseInt(request.getParameter("weight")));
		    }
		    catch(NumberFormatException exception){
				PrintWriter rw = response.getWriter();
				rw.append("<html><head>" +
		         "<meta http-equiv=\"Content-Type\"" + 
		            "content=\"text/html; charset=windows-1256\">" +
		         "<title>  Error - Adventure Time MedSys  </title>" +
		         "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" +
		      "</head><body><center><br><br>The weight must be a numerical value.<br>Please press back on your browser</center></body></html>");
				rw.close();
		    }
		    
		    // attempt to parse and set the date
		    try {
				DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
				newRecord.setDate((Date)formatter.parse(request.getParameter("date")));
			} catch (ParseException e) {
				PrintWriter rw = response.getWriter();
				rw.append("<html><head>" +
		         "<meta http-equiv=\"Content-Type\"" + 
		            "content=\"text/html; charset=windows-1256\">" +
		         "<title>  Error - Adventure Time MedSys  </title>" +
		         "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" +
		      "</head><body><center><br><br>The date must be in the proper format.  Select using the calendar.<br>Please press back on your browser</center></body></html>");
				rw.close();				
			}
		    newRecord.setRecommendations(request.getParameter("recommendations"));
		    if(request.getParameter("paid").equalsIgnoreCase("true")){
		    	newRecord.setPaid(true);
		    }
		    else{
		    	newRecord.setPaid(false);
		    }
		    
		    container.store(newRecord);
		    container.commit();
		    
		    response.sendRedirect("treatmentRecordSuccess.jsp");
		    
		}
		catch(Throwable theException){
			System.out.println(theException);
		}
	}

}